<?php

namespace App\Admin\Controllers;

use App\Wish;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class WishController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('用户愿望列表')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑用户愿望')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新建愿望')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Wish);
        $grid->actions(function ($actions) {
            $actions->disableView();
        });
        $grid->disableFilter();
        $grid->disableExport();
        $grid->column('id', 'id');
        $grid->column('content', '愿望内容');
        $states = [
            'on'  => ['value' => 1, 'text' => '已浇水', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '未浇水', 'color' => 'default'],
        ];
        $grid->column('status', '许愿状态')->switch($states);
        $grid->column('created_at', '创建时间');
        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Wish);
        $form->id('id');
        $form->textarea('content', '愿望内容');
        $states = [
            'on'  => ['value' => 1, 'text' => '已浇水', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '未浇水', 'color' => 'default'],
        ];
        $form->switch('status', '状态')->states($states);
        $form->display('created_at', '创建时间');
        return $form;
    }
}
